<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Validate;

class ActivityDiscount extends Model
{
	use \base\model\ModelTrait;

	//自动完成
	protected $auto = [];
	// 自动完成, 只在新增的时候有效
	protected $insert = [];
	// 自动完成, 只在更新的时候有效
	protected $update = [];
	//只读字段
	protected $readonly = [];

	/**
    * 错误信息
    */
	public $errorInfo;
	/**
    * 验证规则
    */
	protected $rule = [
//		'shop_id'=>'require|integer',
//		//'goods_id'=>'require',
		'name'=>'require',
//    	'price' => 'require|float|min:0',
    	'start_time' => 'require|date',
    	'stop_time' => 'require|date|gt:start_time',
	];
	
	/**
     * 删除
     * @param type $user_id
     * @param type $new_pwd
     * @return type
     */
	public function setDel($ids,$v)
    {
        echo $this->where('id','in', $ids)->fetchSql(true)->update(['is_del' => $v]);die;
    	return $this->where('id','in', $ids)->update(['is_del' => $v]);
    }

	/**
     * 设置启用
     * @param type $ids
     * @param type $v
     * @return type
     */
	public function setEnable($ids,$v)
    {
    	return $this->where('id','in', $ids)->update(['status' => $v]);
    }
	
	
	/**
     * 获取用户对订单可用的活动
     * @param type $user_id
     * @param type $new_pwd
     * @return type
     */
	public  function beUsableActivity($cartInfo,$price,$shopId=0){
		$shop_ids = array_column($cartInfo, 'shop_id');
		$goods_ids = array_column($cartInfo, 'goods_id');
		$cate_ids = [];
		foreach($cartInfo as $key => $value){
			$cate_ids[] = $value['goods']['cate_id'];
		}
    	$model = new self;
    	$model = $model->where('is_del',0);
    	$model = $model->where('status',1);
    	$model = $model->where('benefit_cond','<=',$price);
		$model = $model->where(function ($query) use($shop_ids,$shopId) {
			$query->whereor('shop_id','');
			foreach ($shop_ids as $key => $value) {
				if($value==0){
					$query->whereor([['','exp',\Db::raw("find_in_set($shopId,shop_id)")]]);
				}else{
					$query->whereor([['','exp',\Db::raw("find_in_set($value,shop_id)")]]);
				}
			}
		});
	    $model = $model->where('start_time','< time',date("Y-m-d H:i:s"));
	    $model = $model->where('stop_time','> time', date("Y-m-d H:i:s"));
		$model = $model->where(function ($query) use($goods_ids,$cate_ids) {
			$query->where('scope',0);
			$query->whereor(function ($query) use($goods_ids) {
				$query->where('scope',1);
				$query->where(function ($query) use($goods_ids) {
					foreach ($goods_ids as $key => $value) {
						$query->where([['','exp',\Db::raw("find_in_set($value,goods_scope)")]]);
					}
				});
			});
			$query->whereor(function ($query) use($cate_ids) {
				$query->where('scope',2);
				$query->where(function ($query) use($cate_ids) {
					foreach ($cate_ids as $key => $value) {
						$query->where([['','exp',\Db::raw("find_in_set($value,goods_cate_scope)")]]);
					}
				});
			});
		});
		$model = $model->field("
			status,scope,goods_scope,goods_cate_scope,
			is_del,add_time,start_time,stop_time
		",true);
		$model = $model->order('benefit_money', 'desc');
		$model = $model->order('benefit_discount', 'asc');
		return $model->select()->toArray();
    }
	

	
}







